package com.yaodian100.ec.product.web;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.xwork.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import com.yaodian100.core.common.dao.impl.CommonCriteria;
import com.yaodian100.core.common.entity.SimplePager;
import com.yaodian100.ec.category.entity.Category;
import com.yaodian100.ec.category.service.CategoryService;
import com.yaodian100.ec.generate.service.GenerateService;
import com.yaodian100.ec.product.entity.ProdCatRC;
import com.yaodian100.ec.product.service.ProductRCService;

/**
 * 
 * @author aaron.chueh
 *
 */
public class ProductRCAction extends ActionSupport implements Preparable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -8308247319889420336L;
	private final Logger logger = LoggerFactory.getLogger(ProductRCAction.class);
	
	@Autowired
	private ProductRCService productRCService;
	@Autowired
	private CategoryService categoryService;
	@Autowired
	private GenerateService generateService;
	
	//prepare()
	private List<Category> categoryRoot;
	
	//loadSelBar()
	private String selCategoryId;
	private String jsons;
	
	//list()
	private String categoryId;
	private String categoryName;
	private List productRCList;
	
	//productSearch()
	private SimplePager pager;
	private String keyword;
	private List productList;
	
	//delete()
	private String[] delPrdRCIds;
	
	private String categorySelect;
	private String elementSelect;
	private String attributeSelect;
	private String itemSelect;
	private String unitSelect;
	private String listSelect;
	
	@Override
	public void prepare() throws Exception {
		if (this.categoryRoot == null) {
			this.categoryRoot = categoryService.getAvailableChild(new Long(0));
		}
		
		if (pager == null) {
			pager = new SimplePager();
		}
		pager.setPageRecord(10);//设定商品查询每页显示10笔
	}
	
	public String loadSelBar() throws Exception {
		StringBuffer sb=new StringBuffer();
		if(selCategoryId!=null && !selCategoryId.equals("-1")) {
			sb.append("[");
			List<Category> categories=categoryService.getAvailableChild(Long.parseLong(selCategoryId));
			for(int i=0 ; i<categories.size() ; i++){
				Category ctry=categories.get(i);
				sb.append("\"");
				sb.append(ctry.getId()+":"+ctry.getName());
				if(i==categories.size()-1){
					sb.append("\"");
				}else{
					sb.append("\",");
				}
			}
			sb.append("]");
		}else{
			sb.append("[]");
		}
		jsons=sb.toString();
		
		return "loadSelBar";
	}
	
	public String list() throws Exception {
		logger.debug("categoryId:"+categoryId);
		
		if(StringUtils.isNotBlank(categoryId)){
			Category category=categoryService.getCategoryById(Long.parseLong(categoryId));
			categoryName=category.getName();
			
			CommonCriteria crit=new CommonCriteria();
			crit.addEq("categoryId",categoryId);
			String[] sortArray=new String[]{"sortOrder asc"};
			productRCList=productRCService.getList(0, -1, crit, sortArray);
		}
		
		return "list";
	}
	
	public String productSearch() throws Exception {
		int totalCount=productRCService.getProductCount(categoryId, keyword);
		pager.setTotalSize(Long.parseLong(totalCount+""));
		
		int endRow=(pager.getCurrentPage()+1)*pager.getPageRecord();
		int startRow=(pager.getCurrentPage()*pager.getPageRecord())+1;
		productList=productRCService.getProductList(categoryId, keyword, endRow, startRow);
		
		return "productSearch";
	}
	/*
	public String ajaxDraft() throws Exception {
		HttpServletRequest request=(HttpServletRequest)ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
		List<ProdCatRC> prodCatRCList=this.getProdCatRCList(request);
		productRCService.dftProductsRC(prodCatRCList);
		
		return null;
	}
	*/
	public String delete() throws Exception {
		productRCService.delProductsRC(delPrdRCIds);
		
		return "redirectList";
	}
	
	public String draft() throws Exception {
		HttpServletRequest request=(HttpServletRequest)ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
		List<ProdCatRC> prodCatRCList=this.getProdCatRCList(request);
		productRCService.dftProductsRC(prodCatRCList);
		
		return "redirectList";
	}

	public String save() throws Exception {
		HttpServletRequest request=(HttpServletRequest)ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
		List<ProdCatRC> prodCatRCList=this.getProdCatRCList(request);
		productRCService.saveProductsRC(prodCatRCList);
		
		generateService.generateCuratorRecommV3(categoryId);
		
		return "redirectList";
	}
	
	public List<ProdCatRC> getProdCatRCList(HttpServletRequest request){
		List<ProdCatRC> prodCatRCList=new ArrayList();
		for(int i=0 ; i<10 ; i++){
			String id=request.getParameter("id_"+i);
			if(id!=null){
				ProdCatRC prodCatRC;
				if(id.equals("")){
					prodCatRC=new ProdCatRC();
				}else{
					prodCatRC=productRCService.get(id);
				}
				
				prodCatRC.setId(id);
				prodCatRC.setProductId(request.getParameter("productId_"+i));
				prodCatRC.setProductName(request.getParameter("productName_"+i));
				prodCatRC.setCategoryId(request.getParameter("prdCateId_"+i));
				prodCatRC.setSortWeight(request.getParameter("sortWeight_"+i));
				prodCatRC.setAdvTitle(request.getParameter("advTitle_"+i));
				prodCatRC.setAdvStyle(request.getParameter("advStyle_"+i));
				prodCatRC.setCategoryPath(request.getParameter("categoryPath_"+i));
				prodCatRC.setPromoteName(request.getParameter("promoteName_"+i));
				prodCatRC.setShelfPrice(request.getParameter("shelfPrice_"+i));
				prodCatRC.setListPrice(request.getParameter("listPrice_"+i));
				
				prodCatRCList.add(prodCatRC);
			}
		}
		
		return prodCatRCList;
	}

	//getter & setter
	public ProductRCService getProductRCService() {
		return productRCService;
	}

	public void setProductRCService(ProductRCService productRCService) {
		this.productRCService = productRCService;
	}

	public CategoryService getCategoryService() {
		return categoryService;
	}

	public void setCategoryService(CategoryService categoryService) {
		this.categoryService = categoryService;
	}

	public GenerateService getGenerateService() {
		return generateService;
	}

	public void setGenerateService(GenerateService generateService) {
		this.generateService = generateService;
	}

	public List<Category> getCategoryRoot() {
		return categoryRoot;
	}

	public void setCategoryRoot(List<Category> categoryRoot) {
		this.categoryRoot = categoryRoot;
	}

	public String getSelCategoryId() {
		return selCategoryId;
	}

	public void setSelCategoryId(String selCategoryId) {
		this.selCategoryId = selCategoryId;
	}

	public String getJsons() {
		return jsons;
	}

	public void setJsons(String jsons) {
		this.jsons = jsons;
	}

	public List getProductRCList() {
		return productRCList;
	}

	public void setProductRCList(List productRCList) {
		this.productRCList = productRCList;
	}

	public String getCategoryId() {
		return categoryId;
	}

	public void setCategoryId(String categoryId) {
		this.categoryId = categoryId;
	}

	public String getCategoryName() {
		return categoryName;
	}

	public void setCategoryName(String categoryName) {
		this.categoryName = categoryName;
	}

	public SimplePager getPager() {
		return pager;
	}

	public void setPager(SimplePager pager) {
		this.pager = pager;
	}

	public String getKeyword() {
		return keyword;
	}

	public void setKeyword(String keyword) {
		this.keyword = keyword;
	}

	public List getProductList() {
		return productList;
	}

	public void setProductList(List productList) {
		this.productList = productList;
	}

	public String[] getDelPrdRCIds() {
		return delPrdRCIds;
	}

	public void setDelPrdRCIds(String[] delPrdRCIds) {
		this.delPrdRCIds = delPrdRCIds;
	}

	public String getCategorySelect() {
		return categorySelect;
	}

	public void setCategorySelect(String categorySelect) {
		this.categorySelect = categorySelect;
	}

	public String getElementSelect() {
		return elementSelect;
	}

	public void setElementSelect(String elementSelect) {
		this.elementSelect = elementSelect;
	}

	public String getAttributeSelect() {
		return attributeSelect;
	}

	public void setAttributeSelect(String attributeSelect) {
		this.attributeSelect = attributeSelect;
	}

	public String getItemSelect() {
		return itemSelect;
	}

	public void setItemSelect(String itemSelect) {
		this.itemSelect = itemSelect;
	}

	public String getUnitSelect() {
		return unitSelect;
	}

	public void setUnitSelect(String unitSelect) {
		this.unitSelect = unitSelect;
	}

	public String getListSelect() {
		return listSelect;
	}

	public void setListSelect(String listSelect) {
		this.listSelect = listSelect;
	}
}